package com.example.demo.controller;

import com.example.demo.mapper.QuestionMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.Question;
import com.example.demo.model.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * get返回页面post  返回请求
 */
@Controller
public class PublishController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/publish")
    public String publish() {
        System.out.println("/publish");
        System.out.println("get   请求zhong");
        return "publish";
    }


    /*
    传递 标题头
    返回请求

    注入redex

     */
//    @RequestMapping(value="registerForm",method=RequestMethod.GET)
    @RequestMapping(value = "/puslishpost", method = RequestMethod.POST)
//    @PostMapping("/puslish")
    public String doPubLish(@RequestParam(value = "title", required = false) String title,
                            @RequestParam(value = "description", required = false) String description,
                            @RequestParam(value = "tag", required = false) String tag,
                            HttpServletRequest request, Model model) {
        User user = null;
        model.addAttribute("title", title);
        model.addAttribute("description", description);
        model.addAttribute("tag", tag);

        Question question = new Question();   //   申明返回对象
        question.setTitle(title);
        question.setDescription(description);
        question.setTags(tag);
//        System.out.println("user.id====="+user.getId());

        question.setGmtCreate(System.currentTimeMillis());
        question.setGmtModified(question.getGmtCreate());
        if (title == null || title.equals("")) {
            logger.error("title" + title);
            model.addAttribute("error", "title标题不能为null");
        }
        if (description == null || description .equals("")) {
            logger.error("description" + description);
            model.addAttribute("error", "description问题补充不能为null");
        }
        if (tag == null || tag.equals("")) {
            logger.error("tag" + tag);
            model.addAttribute("error", "tag不能为null");
        }


        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("token")) {
                String token = cookie.getValue();
                user = userMapper.findByToken(token);

                if (user != null) {
                    request.getSession().setAttribute("user", user);
                    question.setCreator(user.getId());
                }
                break;
            }

        }
        //  这里user 为空带入错误信息
        if (user == null) {
            model.addAttribute("error", "用户未登陆");
            System.out.println("我是未登陆需要登陆");
            return "publish";
        }
        System.out.println("questtiong====="+question);
        int flage = questionMapper.create(question);// 20201.7.7  视频中当前位置没有通过int  类型进行接受
        if (flage == 1) {//  成功插入数据后返回
            System.out.println("我是返回首页" + flage);
            System.out.println("我是返回首页");
            return "redirect:/";
//            return "/";// 这里返回到首页
        } else {
            System.out.println("失败返回" + flage);
            return "publish";// 这里返回到首页
        }

    }
}
